import wxValidate from 'wechat-validate'
// 获取应用实例
const app = getApp()
Page({
  behaviors: [wxValidate],
  data: {
    countDownVisible: false,
    mobile: '',
    code: ''
  },

  onLoad ({redirectURL}) {
    this.redirectURL = redirectURL
  },

  rules: {
    mobile: [
      { required: true, message: '请填写手机号码！' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码！' }
    ],
    code: [
      { required: true, message: '请填写验证码！' },
      { pattern: /^\d{6}$/, message: '请填写正确的验证码！' }
    ]
  },

  countDownChange(ev) {
    this.setData({
      timeData: ev.detail,
      countDownVisible: ev.detail.minutes === 1 || ev.detail.seconds > 0,
    })
  },
  // 获取短信验证码
  async getSMSCode () {
    const {valid, message} = this.validate('mobile')
    if (!valid) return wx.utils.toast(message)
    this.setData({countDownVisible: true})
    const {code, data, message: msg} = await wx.http.get('/code', { mobile: this.data.mobile })
    if (code !== 10000) return wx.utils.toast('发送失败，稍后重试！')
    this.setData({
      code: data.code
    })
    wx.utils.toast(msg)
  },
  async submitForm () {
    if (!this.validate()) return
    const {code, data, message} = await wx.http.post('/login', {
      mobile: this.data.mobile,
      code: this.data.code
    })
    if (code !== 10000) return wx.utils.toast(message)
    app.setToken('token', data.token)
    app.setToken('refreshToken', data.refreshToken)
    wx.reLaunch({
      url: this.redirectURL
    })
  }
})
